package vn.gq.tym.storytemplate;

import vn.gq.udv.utils.ButtonHelper;
import vn.gq.udv.utils.SharePreferenceUtil;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class IntroActivity extends BaseActivity {

	private final String TAG = IntroActivity.class.getSimpleName();
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		GAManager.newInstance(this).track(getString(R.string.app_name));
		setContentView(R.layout.firstscr);
		String author = getString(R.string.author_name);
		if (!author.trim().equals(""))
			getSupportActionBar().setSubtitle(author);
		Button btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				exit();
			}
		});
		
		Button btnMoreApp = (Button) findViewById(R.id.btnStore);
		btnMoreApp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				new ButtonHelper(IntroActivity.this).showMoreApp();
			}
		});
		
		Button btnRead = (Button) findViewById(R.id.btnRead);
		btnRead.setText(GQConst.TYPE_BOOK ? "Đọc Sách" : "Đọc Truyện");
		btnRead.setOnClickListener(new View.OnClickListener() {
			
			@SuppressWarnings("unused")
			@Override
			public void onClick(View v) {
//				if (GQConst.SHOW_CATEGORY && SharePreferenceUtil.getInstance(IntroActivity.this).getBoolean(CategoryActivity.PREF_STARTCATEGORYACTIVITY, true))
//					startActivity(new Intent(IntroActivity.this, CategoryActivity.class));
//				else
//					startActivity(new Intent(IntroActivity.this, ChapterActivity.class));
				
				startActivity(new Intent(IntroActivity.this, MainActivity.class));
			}
		});
		
		((Button) findViewById(R.id.btnContact)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(Intent.ACTION_SENDTO);
//				Uri uri = Uri.parse(GQConst.EMAIL);
//				intent.setData(uri);
//				startActivity(Intent.createChooser(intent, "Send mail..."));
				sendContact();
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.none, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void sendContact() {
		PackageInfo pkgInfo = null;
		try {
			pkgInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		String mEmailBody = String.format("\n\n\n\n\n\n" +
				"-------------------" +
				"\nTên ứng dụng: %s\nMã ứng dụng: %s", getString(R.string.app_name), getPackageName());
		if (pkgInfo != null) {
			mEmailBody += String.format("\nMã phiên bản: %d", pkgInfo.versionCode);
		}
		mEmailBody += "\n-------------------";
		
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.putExtra(Intent.EXTRA_SUBJECT, "Góp ý, nhận xét, yêu cầu truyện");
		intent.putExtra(Intent.EXTRA_TEXT, mEmailBody);
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "ungdungviet.llc@gmail.com" });
		intent.setType("message/rfc822");
		try {
			startActivity(Intent.createChooser(intent, "Chọn ứng dụng gửi mail"));
		} catch(ActivityNotFoundException e) {
			Toast.makeText(this, "Không tìm thấy ứng dụng email trong máy", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onBackPressed() {
		exit();
	}

	public void exit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(IntroActivity.this);
		builder.setMessage("Hãy dành chút thời gian đánh giá cho ứng dụng!")
		.setCancelable(true)
		.setTitle("ỨNG DỤNG VIỆT")
		.setIcon(android.R.drawable.btn_star_big_on)
		.setPositiveButton("Đánh giá", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				try {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(GQConst.DETAILURI)));
				} catch(ActivityNotFoundException e) {
					Toast.makeText(IntroActivity.this, "Ứng dụng Google Play không được cài trên máy", Toast.LENGTH_SHORT).show();
				}
			}
		})
		.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onDestroy();
				System.runFinalizersOnExit(true);
				System.exit(0);
			}
		});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

}
